package com.szqhyxj.youxiangju.biz.partnerHome.my;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BasePromptFragment;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.core.UserInfo;

import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.merchantHome.MerchantContainerActivity;
import com.szqhyxj.youxiangju.biz.personalInformation.PersonalInformationActivity;

import com.szqhyxj.youxiangju.biz.partnerHome.shop.order.PartnerOrderListActivity;
import com.szqhyxj.youxiangju.biz.set.SettingActivity;
import com.szqhyxj.youxiangju.biz.web.WebActivity;
import com.szqhyxj.youxiangju.sys.components.HeadportraitView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by leung on 2018/10/31.
 */

public class PartnerMyFragment extends BasePromptFragment implements PartnerMyContract.View, PartnerIconAdapter.IconClickListener {

    @BindView(R.id.avatar)
    HeadportraitView avatar;
    @BindView(R.id.username)
    TextView username;
    @BindView(R.id.partnericon)
    ImageView partnericon;
    @BindView(R.id.inviteqrcode)
    ImageButton inviteqrcode;
    @BindView(R.id.balance)
    TextView balance;
    @BindView(R.id.funcs)
    RecyclerView funcs;
    Unbinder unbinder;
    @BindView(R.id.gomerchant)
    TextView gomerchant;
    private PartnerMyPresenter presenter;

    public static PartnerMyFragment newInstance() {
        Bundle args = new Bundle();
        PartnerMyFragment fragment = new PartnerMyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (presenter == null) {
                presenter = new PartnerMyPresenter(this);
                presenter.init();
            }
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_partner_my;
    }

    @Override
    protected void start(){
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void showMyFuncItems(List<PartnerMyModel.IconItem> iconItems) {
        funcs.setOverScrollMode(View.OVER_SCROLL_NEVER);
        funcs.setLayoutManager(new GridLayoutManager(getContext(), 3));
        funcs.setAdapter(new PartnerIconAdapter(iconItems, this));
    }

    @Override
    public void showUserInfo(UserInfo userInfo) {
        username.setText(userInfo.getUsername());

    }

    @Override
    public void showPartner() {

    }

    @Override
    public void showShape() {
        inviteqrcode.setVisibility(View.VISIBLE);
    }

    @Override
    public void enshowPartner() {

    }

    @Override
    public void enshowShape() {

    }

    @Override
    public void showBalance(String s) {
        if (s.equals("0")) {
            balance.setText(s + ".00");
        } else {
            String strm = s.substring(0, s.length() - 2);
            balance.setText(strm);
        }

    }

    @Override
    public void showCertification() {

    }

    @Override
    public void enshowCertification() {

    }

    @Override
    public void showGoMerchant() {
        gomerchant.setVisibility(View.VISIBLE);
    }

    @Override
    public void enshowGoMerchant() {
        gomerchant.setVisibility(View.GONE);
    }

    @Override
    public void onClickItem(PartnerMyModel.ICON icon) {
        if (icon == PartnerMyModel.ICON.DEVICE_ORDER) {
            PartnerOrderListActivity.start(getActivity());
        }
        if (icon == PartnerMyModel.ICON.POWERATTORNEY) {
            String path = String.format("authorization.html?username=%s&inviteCode=%s&date=%s", UserInfoManger.INSTANCE.getUser().getUsername(), UserInfoManger.INSTANCE.getUser().getInvitePhone(), "2018年02月02日");
            WebActivity.startToH5(getActivity(), "授权书", path);
        }
        if (icon == PartnerMyModel.ICON.TEAM) {

        }
        if (icon == PartnerMyModel.ICON.CONTACT) {
            String serviceNumber = UserInfoManger.INSTANCE.getUser().getServiceNumber();
            Intent intent = new Intent(Intent.ACTION_DIAL);
            Uri data = Uri.parse("tel:" + serviceNumber);
            intent.setData(data);
            startActivity(intent);
        }
        if (icon == PartnerMyModel.ICON.FEEDBACK) {

        }
        if (icon == PartnerMyModel.ICON.SETTING) {
            SettingActivity.start(getActivity());
        }
    }

    @OnClick({R.id.avatar, R.id.gomerchant, R.id.inviteqrcode})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.avatar:
                PersonalInformationActivity.start(getActivity());
                break;
            case R.id.gomerchant:
                MerchantContainerActivity.start(getActivity());
                getActivity().finish();
                break;
            case R.id.inviteqrcode:

                String path =  UserInfoManger.INSTANCE.getUser().getUseraccount()+"invite";
                WebActivity.startToH5(getActivity(), "推荐好友", path);
                break;
        }
    }
}